다음으로 그라데이션 효과를 위한 GradientPaint 클래스를 살펴보자.
☞ GradientPaint 클래스의 유용한 생성자 |
public GradientPaint(float x1, float y1, Color color1,
float x2, float y2, Color color2 ) |
시작점(x1, y1)의 색(color1)에서 끝점(x2, y2)의 색(color2)으로 변하는 Paint 객체를 만든다. |
public GradientPaint(float x1, float y1, Color color1,
float x2, float y2, Color color2, boolean cyclic) |
시작점(x1, y1)의 색(color1)에서 끝점(x2, y2)의 색(color2)으로 변하는 Paint 객체를 만든다. cyclic이 true이면 반복적으로 그라데이션 효과가 적용된다. |
GradientPaint 객체를 생성하고 Graphics2D 객체에 적용하는 코드를 예로 들면 아래와 같다.
g2.setPaint( new GradientPaint(0, 0, Color.white, 50, 50, Color.blue, true) ); |
다음은 프레임의 생성자에서 네 개의 GradientPaint 객체를 생성한 후에 paint 메소드에서 GradientPaint 속성을 적용하여 사각형을 그리는 예제이다.
import java.awt.*;
import java.awt.geom.*;
public class Gradient1 extends Frame{
Paint gPaint1, gPaint2, gPaint3, gPaint4; // Paint 속성을 위한 변수들
public Gradient1(String title){ // 생성자
super(title);
// GradientPaint 객체를 생성한다.
gPaint1=new GradientPaint(10, 50, Color.white, 20, 60, Color.blue, true);
gPaint2=new GradientPaint(100, 50, Color.white, 100, 30, Color.blue, true);
gPaint3=new GradientPaint(190, 50, Color.white, 270, 250, Color.blue, false);
gPaint4=new GradientPaint(280, 50, Color.white, 280, 250, Color.blue, false);
}
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
// GradientPaint를 적용하여 사각형을 그린다.
g2.setPaint(gPaint1);
g2.fill(new Rectangle2D.Double(10,50,80,200));
g2.setPaint(gPaint2);
g2.fill(new Rectangle2D.Double(100,50,80,200));
g2.setPaint(gPaint3);
g2.fill(new Rectangle2D.Double(190,50,80,200));
g2.setPaint(gPaint4);
g2.fill(new Rectangle2D.Double(280,50,80,200));
}
public static void main(String []args){
Frame f=new Gradient1("그라데이션 효과");
f.setSize(400,300);
f.setVisible(true);
}
}
[그림 18-10] 그라데이션 효과
마
지막으로 TexturePaint는 작은 그림을 반복적으로 채우기 위한 클래스이다. 작은 그림으로 BufferedImage 객체를
사용하는데 BufferedImage 클래스는 이미지의 각 픽셀의 정보를 버퍼에 기억하고 있어 프로그래머는 그 버퍼에 직접
접근하여 이미지의 정보를 바꿀 수 있다. 따라서 BufferedImage를 사용하면 이미지를 다양하게 처리할 수 있다
출처 : http://tong.nate.com/samusahan/21711839
|